# APPENDIX FIGURES
# Authors: Mark Kayser & Kasia Nalewajko
# First created: Feb 9, 2023
# Replicated: Nov 18, 2025

rm(list = ls())

# LOAD PACKAGES -----------------------------------------------------------

if (!require("dplyr")) install.packages("dplyr")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("estimatr")) install.packages("estimatr")
if (!require("marginaleffects")) install.packages("marginaleffects")

# LOAD DATA ---------------------------------------------------------------

setwd("set/path/to/folder")

load(file = "./01 data/02 generated data/study1/01results_fullsample_clean.Rda")
results1 <- results %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1)))

load(file = "./01 data/02 generated data/study2/02results_fullsample_clean.Rda")
results2 <- results %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check == 3)


# PREP VARS ----

results1 <- results1 %>% 
  mutate(PiS_or_PO_and_party_id = PiS_or_PO,
         PiS_or_PO_and_party_id = ifelse(is.na(PiS_or_PO_and_party_id) & party_id == 3, 1, PiS_or_PO_and_party_id),
         PiS_or_PO_and_party_id = ifelse(PiS_or_PO_and_party_id == 1, "PiS", as.character(PiS_or_PO_and_party_id)),
         PiS_or_PO_and_party_id = ifelse(PiS_or_PO_and_party_id == 2, "PO", as.character(PiS_or_PO_and_party_id)),
         PiS_or_PO_and_party_id = ifelse(PiS_or_PO_and_party_id == 999, NA, as.character(PiS_or_PO_and_party_id)))

results2 <- results2 %>% 
  mutate(PiS_or_PO_and_party_id = PiS_or_PO,
         PiS_or_PO_and_party_id = ifelse(is.na(PiS_or_PO_and_party_id) & party_id == 3, 1, PiS_or_PO_and_party_id),
         PiS_or_PO_and_party_id = ifelse(is.na(PiS_or_PO_and_party_id) & party_id == 15, 2, PiS_or_PO_and_party_id),
         PiS_or_PO_and_party_id = ifelse(PiS_or_PO_and_party_id == 1, "PiS", as.character(PiS_or_PO_and_party_id)),
         PiS_or_PO_and_party_id = ifelse(PiS_or_PO_and_party_id == 2, "PO", as.character(PiS_or_PO_and_party_id)),
         PiS_or_PO_and_party_id = ifelse(PiS_or_PO_and_party_id == 999, NA, as.character(PiS_or_PO_and_party_id)))

# PLOT A1 ----

# TOP PANE (Study 1)

results1 %>% 
  filter(!is.na(popis)) %>% 
  filter(!is.na(PiS_or_PO_and_party_id)) %>% 
  group_by(as.factor(popis), as.factor(PiS_or_PO_and_party_id)) %>% 
  summarise(count = n(),
            perc = n()/1038 * 100)

# PiS PiS = 309/(309+28) 92%
# PiS PO = 28/(309+28) 8%
# PO PO = 694/(694+9) 99%
# PO PiS = 9/(694+9) 1%

labels_df <- data.frame(
  popis = c("PiS", "PiS",  "PO", "PO"),
  PiS_or_PO_and_party_id = c("PiS", "PO", "PiS", "PO"),
  perc_label = c("92%", "8%", "1%", "99%")
)

results1 %>% 
  filter(!is.na(popis)) %>% 
  filter(!is.na(PiS_or_PO_and_party_id)) %>% 
  ggplot() +
  geom_point() +
  aes(x = as.factor(popis)) +
  aes(y = as.factor(PiS_or_PO_and_party_id)) +
  geom_jitter(width = 0.2, height = 0.2, alpha = 0.6) +
  # scale_y_discrete(labels = c("1" = "PiS", "2" = "PO")) +
  geom_text(data = labels_df,
            aes(label = perc_label),
            color = "black", size = 5, fontface = "bold",
            nudge_y = 0.3) +
  labs(x = "Result of the 4-step 'popis' variable classification",
       y = "Result of 'party identification' and 'if you had to...' questions",
       title = "Study 1") +
  theme_bw()

# BOTTOM PANE (Study 2)

results2 %>% 
  filter(!is.na(popis)) %>% 
  filter(!is.na(PiS_or_PO_and_party_id)) %>% 
  group_by(as.factor(popis), as.factor(PiS_or_PO_and_party_id)) %>% 
  summarise(count = n(),
            perc = n()/1293 * 100)

# PiS PiS = 399/(399+39) 91%
# PiS PO = 39/(399+39) 9%
# PO PO = 838/(838+17) 98%
# PO PiS = 17/(838+17) 2%

labels_df <- data.frame(
  popis = c("PiS", "PiS",  "PO", "PO"),
  PiS_or_PO_and_party_id = c("PiS", "PO", "PiS", "PO"),
  perc_label = c("91%", "9%", "2%", "98%")
)

results2 %>% 
  filter(!is.na(popis)) %>% 
  filter(!is.na(PiS_or_PO_and_party_id)) %>% 
  ggplot() +
  geom_point() +
  aes(x = as.factor(popis)) +
  aes(y = as.factor(PiS_or_PO_and_party_id)) +
  geom_jitter(width = 0.2, height = 0.2, alpha = 0.6) +
  # scale_y_discrete(labels = c("1" = "PiS", "2" = "PO")) +
  geom_text(data = labels_df,
            aes(label = perc_label),
            color = "black", size = 5, fontface = "bold",
            nudge_y = 0.3) +
  labs(x = "Result of the 4-step 'popis' variable classification",
       y = "Result of 'party identification' and 'if you had to...' questions",
       title = "Study 2") +
  theme_bw()

# PLOT A2 -----

temp <- results1 %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1)))

main_triple_interaction <- lm_robust(formula = outcome ~ exp_condition*as.factor(popis)*GSparty_id_z + experiment_first + as.factor(voivodeship),
                                     data = temp)

marginaleffects::plot_slopes(main_triple_interaction,
                             variables = "exp_condition",
                             condition = list("GSparty_id_z", "popis"),
                             conf_level = 0.95
                             # ,newdata = temp
                             ) +
  theme_minimal() +
  labs(y = "Marginal effects",
       x = "Strength of partisanship (Graham & Svolik 2020)") +
  theme(
    plot.title = element_text(size = 15, face="bold"),
    plot.subtitle = element_text(size = 14, face="italic"),
    axis.title.x = element_text(hjust=1),
    axis.title.y = element_text(hjust=0.5))


# PLOT A3 ----

# LEFT PANE: PO supporters who want to rerun the election

# Run the logit model

# recode outcome
results1 <- results1 %>% 
  filter(outcome != 99) %>%
  mutate(outcome_supports = ifelse(outcome > 5, 1, 0))

temp <- results1 %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1))) %>%  
  filter(popis == "PO",
         !is.na(GSparty_id_z))

modelPO <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(modelPO)

# Plot

plot_predictions(modelPO,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PO supporters, PiS wins",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to rerun the election") +
  theme_minimal() +
  scale_color_manual(values = c("PiSwins" = "blue", "POwins" = "transparent", "control" = "transparent")) +
  scale_fill_manual(values = c("PiSwins" = "blue", "POwins" = "transparent", "control" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.05, 1.05)


# RIGHT PANE: PiS supporters who want to rerun the election

temp <- results1 %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1))) %>%  
  filter(popis == "PiS",
         !is.na(GSparty_id_z))

modelPiS <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(modelPiS)

plot_predictions(modelPiS,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PiS supporters, PO wins",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to rerun the election") +
  theme_minimal() +
  scale_color_manual(values = c("POwins" = "red", "PiSwins" = "transparent", "control" = "transparent")) +
  scale_fill_manual(values = c("POwins" = "red", "PiSwins" = "transparent", "control" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.05, 1.05)

# PLOT A4 ----

temp <- results2 %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check == 3)

main_triple_interaction <- lm_robust(formula = outcome ~ exp_condition*as.factor(popis)*GSparty_id_z + experiment_first + as.factor(voivodeship),
                                     data = temp)

marginaleffects::plot_slopes(main_triple_interaction,
                             variables = "exp_condition",
                             condition = list("GSparty_id_z", "popis"),
                             conf_level = 0.95
                             ) +
  theme_minimal() +
  labs(y = "Marginal effects",
       x = "Strength of partisanship (Graham & Svolik 2020)") +
  theme(
    plot.title = element_text(size = 15, face="bold"),
    plot.subtitle = element_text(size = 14, face="italic"),
    axis.title.x = element_text(hjust=1),
    axis.title.y = element_text(hjust=0.5))



# PLOT A5 -----

# LEFT PANE: PO supporters who support selling the port

# Logit model

# recode outcome
results2 <- results2 %>% 
  filter(outcome != 99) %>%
  mutate(outcome_supports = ifelse(outcome > 5, 1, 0))

temp <- results2 %>% 
  filter(popis == "PO",
         !is.na(GSparty_id_z))

modelPO <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(modelPO)


# Plot

plot_predictions(modelPO,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PO supporters, PO party wants to sell",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to sell the port") +
  theme_minimal() +
  scale_color_manual(values = c("ChinaPO" = "blue", "ChinaPiS" = "transparent", "China" = "transparent")) +
  scale_fill_manual(values = c("ChinaPO" = "blue", "ChinaPiS" = "transparent", "China" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.1, 1)

# RIGHT PANE: PiS supporters who support selling the port

temp <- results2 %>% 
  filter(popis == "PiS",
         !is.na(GSparty_id_z))

modelPiS <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(modelPiS)

plot_predictions(modelPiS,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PiS supporters, PiS party wants to sell",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to sell the port") +
  theme_minimal()+
  scale_color_manual(values = c("ChinaPO" = "transparent", "ChinaPiS" = "red", "China" = "transparent")) +
  scale_fill_manual(values = c("ChinaPO" = "transparent", "ChinaPiS" = "red", "China" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.2, 1)

# PLOT A6 ----

temp <- results1 %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1)))

main_triple_interaction <- lm_robust(formula = outcome ~ exp_condition*as.factor(popis)*GSparty_id_z + experiment_first + as.factor(voivodeship),
                                     data = temp)

marginaleffects::plot_slopes(main_triple_interaction,
                             variables = "exp_condition",
                             condition = list("GSparty_id_z", "popis"),
                             conf_level = 0.95
                             ) +
  theme_minimal() +
  labs(y = "Marginal Effects",
       x = "Strength of partisanship (Graham & Svolik 2020)",
       color = "Party ID",
       fill  = "Party ID") +
  theme(
    plot.title = element_text(size = 15, face="bold"),
    plot.subtitle = element_text(size = 14, face="italic"),
    axis.title.x = element_text(hjust=1),
    axis.title.y = element_text(hjust=0.5))


# PLOT A7 ----

temp <- results2 %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check == 3)

main_triple_interaction <- lm_robust(formula = outcome ~ exp_condition*as.factor(PiS_or_PO_and_party_id)*GSparty_id_z + experiment_first + as.factor(voivodeship),
                                     data = temp)

marginaleffects::plot_slopes(main_triple_interaction,
                             variables = "exp_condition",
                             condition = list("GSparty_id_z", "PiS_or_PO_and_party_id"),
                             conf_level = 0.95) +
  theme_minimal() +
  labs(y = "Marginal Effects",
       x = "Strength of partisanship (Graham & Svolik 2020)",
       color = "Party ID",
       fill  = "Party ID") +
  theme(
    plot.title = element_text(size = 15, face="bold"),
    plot.subtitle = element_text(size = 14, face="italic"),
    axis.title.x = element_text(hjust=1),
    axis.title.y = element_text(hjust=0.5))

# PLOT A8 ----

# LEFT PANE: PO supporters who want to rerun the election

# Run the logit model

# recode outcome
results1 <- results1 %>% 
  filter(outcome != 99) %>%
  mutate(outcome_supports = ifelse(outcome > 5, 1, 0))

temp <- results1 %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1))) %>%  
  filter(popis == "PO",
         !is.na(GSparty_id_z))

modelPO <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + LR + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(modelPO)

# Plot

plot_predictions(modelPO,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PO supporters, PiS wins",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to rerun the election") +
  theme_minimal() +
  scale_color_manual(values = c("PiSwins" = "blue", "POwins" = "transparent", "control" = "transparent")) +
  scale_fill_manual(values = c("PiSwins" = "blue", "POwins" = "transparent", "control" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.05, 1.05)

# RIGHT PANE: PiS supporters who want to rerun the election

temp <- results1 %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1))) %>%  
  filter(popis == "PiS",
         !is.na(GSparty_id_z))

modelPiS <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + LR + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(modelPiS)

plot_predictions(modelPiS,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PiS supporters, PO wins",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to rerun the election") +
  theme_minimal() +
  scale_color_manual(values = c("POwins" = "red", "PiSwins" = "transparent", "control" = "transparent")) +
  scale_fill_manual(values = c("POwins" = "red", "PiSwins" = "transparent", "control" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.05, 1.05)


# PLOT A9 ----

temp <- results1 %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check1 == 3 | (is.na(manipulation_check1)))


negativepart_interaction <- lm_robust(formula = outcome ~ exp_condition*popis*GSnegative_partisan_z + experiment_first + as.factor(voivodeship),
                                      data = temp)

marginaleffects::plot_slopes(negativepart_interaction,
                             variables = "exp_condition",
                             condition = list("GSnegative_partisan_z", "popis"),
                             conf_level = 0.95) +
  theme_minimal() +
  labs(y = "Marginal Effects (negative partisanship)",
       x = "Strength of partisanship (Graham & Svolik 2020)",
       color = "Party ID",
       fill  = "Party ID") +
  theme(
    plot.title = element_text(size = 15, face="bold"),
    plot.subtitle = element_text(size = 14, face="italic"),
    axis.title.x = element_text(hjust=1),
    axis.title.y = element_text(hjust=0.5))

# PLOT A10 ----

# LEFT PANEL

temp <- results1 %>% 
  filter(popis == "PO",
         !is.na(GSnegative_partisan_z))

model <- glm(outcome_supports ~ exp_condition*GSnegative_partisan_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(model)
plot_predictions(model,
                 type = "response",
                 condition = c("GSnegative_partisan_z", "exp_condition")) +
  labs(title = "PO supporters, PiS wins",
       x = "Standardised strength of negative partisanship",
       y = "Predicted probability of wanting to rerun the election") +
  theme_minimal() +
  scale_color_manual(values = c("POwins" = "transparent", "PiSwins" = "blue", "control" = "transparent")) +
  scale_fill_manual(values = c("POwins" = "transparent", "PiSwins" = "blue", "control" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.05, 1)


# RIGHT PANEL

temp <- results1 %>% 
  filter(popis == "PiS",
         !is.na(GSnegative_partisan_z))

model <- glm(outcome_supports ~ exp_condition*GSnegative_partisan_z  + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))
summary(model)
plot_predictions(model,
                 type = "response",
                 condition = c("GSnegative_partisan_z", "exp_condition")) +
  labs(title = "PiS supporters, PO wins",
       x = "Standardised strength of negative partisanship",
       y = "Predicted probability of wanting to rerun the election") +
  theme_minimal() +
  scale_color_manual(values = c("POwins" = "red", "PiSwins" = "transparent", "control" = "transparent")) +
  scale_fill_manual(values = c("POwins" = "red", "PiSwins" = "transparent", "control" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.05, 1)

# PLOT A11 ----

results2 %>% 
  filter(int_security_most_important != 99) %>% 
  ggplot() +
  geom_bar() +
  aes(x = as.factor(int_security_most_important)) +
  theme_minimal(base_size = 13, base_family = "Times") +
  labs(title = "Is national security currently the most important political issue facing Poland?", 
       subtitle = "On a scale from 1-10",
       y = "# respondents", 
       x = "") +
  theme(
    plot.title = element_text(size = 15, face="bold"),
    plot.subtitle = element_text(size = 14, face="italic"),
    axis.title.x = element_text(hjust=1),
    axis.title.y = element_text(hjust=0.5))
